package com.tt.fashionWebSite.service.imp;

import com.tt.fashionWebSite.dto.EmailDto;
import com.tt.fashionWebSite.service.Ad_SetupEmailService;
import com.tt.fashionWebSite.service.GenericService;
import com.tt.fashionWebSite.util.CryptographyDataUtils;
import com.tt.fashionWebSite.util.ISSConstants;
import org.ini4j.Profile;
import org.ini4j.Wini;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * Created by isoft-work 5 on 12/5/2014.
 */
@Service("ad_SetupEmailService")
public class Ad_SetupEmailServiceImp extends GenericService implements Ad_SetupEmailService {

   @Override
   public EmailDto getInfo() {
      EmailDto emailDto = new EmailDto();
      try {
         String pathSettingFile = ISSConstants.webappDir.getCanonicalPath() + "/setting.ini";
         File settingFile = new File(pathSettingFile);;
         Wini ini = new Wini(settingFile);

         Profile.Section sec = ini.get("Email");

         if (sec != null && sec.containsKey("Username")) {
            emailDto.setEmail(sec.get("Username"));
         }

         if (sec != null && sec.containsKey("Smtp_Host")) {
            emailDto.setHost(sec.get("Smtp_Host"));
         }

         if (sec != null && sec.containsKey("Smtp_Port")) {
            emailDto.setPort(sec.get("Smtp_Port"));
         }

         if (sec != null && sec.containsKey("Protocol")) {
            emailDto.setProtocol(sec.get("Protocol"));
         }

         if (sec != null && sec.containsKey("Password")) {
            emailDto.setPass(CryptographyDataUtils.decryptWithAES(sec.get("Password")));
         }
      } catch (IOException e) {
         e.printStackTrace();
         emailDto = null;
      }
      return emailDto;
   }

   @Override
   public EmailDto updateInfo(EmailDto email) throws IOException {
      try {
         String pathSettingFile = ISSConstants.webappDir.getCanonicalPath() + "/setting.ini";
         File settingFile = new File(pathSettingFile);
         Wini ini = new Wini(settingFile);

         Profile.Section sec = ini.get("Email");

         if (sec != null && sec.containsKey("Username")) {
            sec.put("Username", email.getEmail());
         }

         if (sec != null && sec.containsKey("Password")) {
            sec.put("Password", CryptographyDataUtils.encryptWithAES(email.getPass()));
         }

         if (sec != null && sec.containsKey("Smtp_Host")) {
            sec.put("Smtp_Host", email.getHost());
         }

         if (sec != null && sec.containsKey("Smtp_Port")) {
            sec.put("Smtp_Port", email.getPort());
         }

         if (sec != null && sec.containsKey("Protocol")) {
            sec.put("Protocol", email.getProtocol());
         }
         ini.store();
      } catch (IOException e) {
         StringWriter sw = new StringWriter();
         e.printStackTrace(new PrintWriter(sw));
         email = null;
      }
         return email;
      }
}
